//
//  HYFolderMoveViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/19.
//

#import "HYFolderMoveViewController.h"
#import "HYFolderMoverBottomView.h"
#import "HYFolderMoveListCell.h"

@interface HYFolderMoveViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *folderSource;

@property (nonatomic, strong) NSString *folderPath;

@property (nonatomic, strong) HYFolderMoverBottomView *bottomView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UITextField *textField;

@end

@implementation HYFolderMoveViewController

- (void)dealloc
{
    CLog(@"dealloc -- %@",[self class]);
}

- (instancetype)initWithFolderSource:(NSArray *)folderSource
{
    self = [super init];
    if(self){
        self.folderSource = [NSMutableArray arrayWithArray:folderSource];
    }
    return self;
}

- (instancetype)initWithFolderSource:(NSArray *)folderSource folderPath:(NSString *)folderPath
{
    self = [super init];
    if(self){
        self.folderSource = [NSMutableArray arrayWithArray:folderSource];
        self.folderPath = folderPath;
    }
    return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = NSLocalizedString(@"移动至", nil);
    [self.navigationBarView.leftButton setImage:[UIImage imageNamed:@"nav_btn_close_n"] forState:UIControlStateNormal];
    [self configUI];
    [self bindSignal];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.viewModel.isFileMove = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.viewModel.isFileMove = NO;
}

- (void)leftButtonClick
{
    [self.viewModel cancelSelectStatus];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)configUI
{
    self.view.backgroundColor = AppBgThemeColor;
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(64+HOME_BAR_HEIGHT);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navigationBarView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    @weakify(self);
    // 新建文件夹
    self.bottomView.newBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self createFolder];
        return [RACSignal empty];
    }];
    // 移动
    self.bottomView.moveBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.viewModel moveToFolderWithCurrentFile:self.fileViewModel 
                                      completeBlock:^{
            @strongify(self);
            [self leftButtonClick];
        }];
        return [RACSignal empty];
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
}

- (void)createFolder
{
    @weakify(self);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"新建文件夹", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        @strongify(self);
        self.textField = textField;
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        if(self.textField.text.length){
            if(self.folderPath){// 本地创建文件夹
                [self.viewModel createFolderWithFolderName:self.textField.text completeBlock:^(id result) {
                    @strongify(self);
                    if(self.viewModel.parentPath.length){
                        [self.folderSource insertObject:result atIndex:1];
                    }else{
                        [self.folderSource insertObject:result atIndex:0];
                    }
                    [self.tableView reloadData];
                }];
            }else{// 数据库创建文件夹
                [self.viewModel addNewFolderWithFolderName:self.textField.text completeBlock:^(id result) {
                    @strongify(self);
                    [self.folderSource insertObject:result atIndex:0];
                    [self.tableView reloadData];
                }];
            }
        }
        self.textField = nil;
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        self.textField = nil;
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.folderSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYFolderMoveListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HYFolderMoveListCell" forIndexPath:indexPath];
    [cell bindViewModel:self.folderSource[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kItemListHeight+10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.viewModel selectMoveToFolderItemViewModel:self.folderSource[indexPath.row]];
    [tableView reloadData];
}

#pragma mark - loadLazy

- (HYFolderMoverBottomView *)bottomView
{
    if(!_bottomView){
        _bottomView = [[HYFolderMoverBottomView alloc]initWithTitles:@[NSLocalizedString(@"新建文件夹", nil),NSLocalizedString(@"移动", nil)]];
    }
    return _bottomView;
}

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.contentInset = UIEdgeInsetsMake(2.5, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[HYFolderMoveListCell class] forCellReuseIdentifier:@"HYFolderMoveListCell"];
    }
    return _tableView;
}

@end
